package org.deri.socializer.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.jivesoftware.smack.packet.Presence;

public class XMPPUtilities {

	public static String getUser(String groupChatMessageFrom) {
		String[] elements = groupChatMessageFrom.split("/");
		String user = groupChatMessageFrom;
		try {
			user = elements[1];
		} catch (Exception exc) {
			
		}
		return user;
	}
	
	
	public static String getRoomName(String from) {
		String[] elements = from.split("@");
		String room = from;
		try {
			room = elements[0];
		} catch (Exception exc) {
			
		}
		return room;
	}
	
	public static String getRoomJid(String from) {
		String[] elements = from.split("/");
		String room = from;
		try {
			room = elements[0];
		} catch (Exception exc) {
			
		}
		return room;
	}
	
	public static String getSenderName(String jid) {
		String[] elements = jid.split("@");
		String sender = jid;
		try {
			sender = elements[0];
		} catch (Exception exc) {
			
		}
		return sender;
	}
	
	public static String getSenderAddress(String jid) {
		String[] elements = jid.split("/");
		String sender = jid;
		try {
			sender = elements[0];
		} catch (Exception exc) {
			
		}
		return sender;
	}
	
	
	public static boolean hasJoined(Presence presence) {
		if (presence.getType().toString().equalsIgnoreCase("available"))
			return true;
		else 
			return false;
	}
	
	
	public static String getRandomUserColor() {
		String hex = "FFFFFF";
		
		Random rnd = new Random(System.currentTimeMillis());
		
		
		
		String hexFull = "ff";
		String hexNone = "00";
		String hexRandom = Integer.toHexString(rnd.nextInt(156));
		if (hexRandom.length() == 0)
			hexRandom = "00";
		else if (hexRandom.length() == 1)
			hexRandom = "0" + hexRandom;
		
		
		List<String> list = new ArrayList<String>();
		list.add(hexFull);
		list.add(hexNone);
		list.add(hexRandom);
		java.util.Collections.shuffle(list);

		hex = "";
		Iterator<String> iter = list.iterator();
		while (iter.hasNext())
			hex += iter.next();
		
		System.out.println(list.toString());
		
		return hex;
	}
	
}
